$(function () {
   obj.init();
});
var dataTest = [
    {
        start: '2019-02-18 18:00:00 ',
        end: '2019-02-18 21:00:00',
        title: '会议1'
    }
];
var currentDate = '';
var obj = new function () {
    'use strict';
    return {
        form:null,
        calendar:null,
        holidayArr:[],
        init:function () {
            this.calendar = $('#calendar');
            currentDate = new Date().format("yyyy-MM-dd");
            this.funObj().loadSchedule([
                {
                    start: '2018-11-03',
                    title:'休息日'
                },
                {
                    start: '2018-11-04',
                    title:'休息日'
                }
            ]);
            this.listener();
        },
        listener:function () {
            var $this = this;

        },
        funObj:function () {
            var $this = this;
            return {
                loadSchedule:function (listData) {
                    // xhExtra.ajaxRequest('', {}, {}, function (res) {
                    //
                    // });
                    $this.funObj().renderSchedule(listData);
                },
                renderSchedule:function (listData) {
                    $this.calendar.fullCalendar('destroy');
                    $this.calendar.fullCalendar({
                        header: {
                            left: 'prev,next',
                            center: 'title',
                            right: 'month'
                        },
                        views: {
                            listDay: { buttonText: '天' },
                            listWeek: { buttonText: '周' }
                        },
                        // height:'100%',
                        height : window.innerHeight-30,
                        windowResize: function(view) {
                            $this.calendar.fullCalendar('option', 'height', window.innerHeight-26);
                            $this.calendar.fullCalendar('option', 'width', 600);
                        },
                        defaultView: 'month',
                        navLinks: false, // can click day/week names to navigate views
                        editable: true,
                        locale:'zh-cn',
                        defaultDate: currentDate,
                        eventLimit: false, // allow "more" link when too many events
                        // dayClick: function(date, allDay, jsEvent, view) {
                        //     var day = date.format();
                        //     var dom = $(this);
                        //     console.log(dom);
                        //     console.log(dom.attr('background-color'));
                        //     // if (dom.hasClass('weekColor')) {
                        //     //     dom.removeClass('weekColor');
                        //     //     $this.holidayArr = $.grep($this.holidayArr, function(value) {
                        //     //         return value != day;
                        //     //     });
                        //     // } else {
                        //     //     dom.addClass('weekColor');
                        //     //     $this.holidayArr.push(day);
                        //     // }
                        //
                        //     // var url = moduleUrl + 'Schedule/addScheduleForm?iUserId='+iUserId;
                        //     // window.top.xhTop.showModal({
                        //     //     title: '添加日程',
                        //     //     width:480,
                        //     //     content: '<iframe frameborder="0" style="width: 100%;height:400px;" src="' + url + '"></iframe>',
                        //     //
                        //     // });
                        // },
                        eventClick: function(event, jsEvent, view) {
                            var str = '';
                            var dayType = 0;
                            if (event.isHoliday == 0) {
                                str = '休息日';
                                dayType = 2;
                                window.top.swal({
                                    title: '修改当天为'+str,
                                    confirmButtonText: '确定',
                                    showCancelButton: true,
                                    cancelButtonText: '取消',
                                    showLoaderOnConfirm: true,
                                    allowOutsideClick: false,
                                    html: '<input id="desc" class="swal2-input" type="text" placeholder="备注信息，非必填"><br/>' +
                                    '<select class="form-control" id="signType" ><option value="inSign">计算打卡</option><option value="outSign">不计算打卡</option></select>',
                                    preConfirm: function() {
                                        return new Promise(function (resolve, reject) {
                                            resolve();
                                        })
                                    }
                                }).then(function () {
                                    var desc = $('#desc', window.top.document).val();
                                    var signType = $('#signType', window.top.document).val();
                                    xhExtra.ajaxRequest('hrm/attendance/saveHoliday',{dayType:dayType,day:event.start.format(),desc:desc,signType:signType},{},function (res) {
                                        window.top.xhTop.showSuccess(res.msg);
                                        $this.calendar.fullCalendar('refetchEvents'); //重新加载日历
                                    })
                                },function (dismiss) {
                                    return false;
                                });
                            } else if (event.isHoliday == 1) {
                                str = '工作日';
                                dayType = 1;
                                window.top.swal({
                                    title: '修改当天为'+str,
                                    confirmButtonText: '确定',
                                    showCancelButton: true,
                                    cancelButtonText: '取消',
                                    showLoaderOnConfirm: true,
                                    allowOutsideClick: false,
                                    input: 'text',
                                    inputPlaceholder:'备注信息，非必填',
                                    preConfirm: function(val) {
                                        return new Promise(function (resolve, reject) {
                                            resolve(val);
                                        })
                                    }
                                }).then(function (val) {
                                    xhExtra.ajaxRequest('hrm/attendance/saveHoliday',{dayType:dayType,day:event.start.format(),desc:val},{},function (res) {
                                        window.top.xhTop.showSuccess(res.msg);
                                        $this.calendar.fullCalendar('refetchEvents'); //重新加载日历
                                    })
                                },function (dismiss) {
                                    return false;
                                });
                            }
                        },
                        events: function (start, end, timezone, callback) {
                            xhExtra.ajaxRequest('hrm/attendance/getHoliday',{startDay:start.format(),endDay:end.format()},{},function (res) {
                                callback.call(this, res);
                            });
                        }
                        // viewRender:function (view,Jquery) {
                        //     // console.log(view);
                        //     // console.log(Jquery);
                        // },
                        // event:function(start, end, callback){
                            //prev上一月, next下一月等事件时调用
                        // }
                    });
                }

            }
        }
    }
};

Date.prototype.format =function(format)
{
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length==1? o[k] :
                ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}